From 7063b199b4748a9c354ed37e64cdc84c512f2c0c Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Thu, 14 Dec 2023 15:30:34 +0100 Subject: refactor(pages): rewrite helpers to output schema in json-ld format * make sure url are absolutes * nest breadcrumb schema in webpage schema * trim HTML tags from content/description * use a regular script instead of next/script (with the latter the schema is not updated on route change) * place the script in document head * add keywords, wordCount and readingTime keys in BlogPosting schema * fix breadcrumbs in search page (without query) * add tests (a `MatchInlineSnapshot` will be better but Prettier 3 is not supported yet) --- src/pages/blog/page/[number].tsx | 57 +++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 27 deletions(-) (limited to 'src/pages/blog/page/[number].tsx') diff --git a/src/pages/blog/page/[number].tsx b/src/pages/blog/page/[number].tsx index 906a08e..fa1123d 100644 --- a/src/pages/blog/page/[number].tsx +++ b/src/pages/blog/page/[number].tsx @@ -3,7 +3,6 @@ import type { ParsedUrlQuery } from 'querystring'; import type { GetStaticPaths, GetStaticProps } from 'next'; import Head from 'next/head'; import { useRouter } from 'next/router'; -import Script from 'next/script'; import { useCallback } from 'react'; import { useIntl } from 'react-intl'; import { @@ -44,13 +43,17 @@ import type { WPTopicPreview, } from '../../../types'; import { CONFIG } from '../../../utils/config'; -import { PAGINATED_ROUTE_PREFIX, ROUTES } from '../../../utils/constants'; import { - getBlogSchema, + ARTICLE_ID, + PAGINATED_ROUTE_PREFIX, + ROUTES, +} from '../../../utils/constants'; +import { + getBlogGraph, getLinksItemData, getPostsWithUrl, - getSchemaJson, - getWebPageSchema, + getSchemaFrom, + getWebPageGraph, } from '../../../utils/helpers'; import { loadTranslation, type Messages } from '../../../utils/helpers/server'; import { @@ -189,21 +192,23 @@ const BlogPage: NextPageWithLayout = ({ messages.pageTitle ); - const webpageSchema = getWebPageSchema({ - description: messages.seo.metaDesc, - locale: CONFIG.locales.defaultLocale, - slug: ROUTES.BLOG, - title: messages.pageTitle, - }); - const blogSchema = getBlogSchema({ - isSinglePage: false, - locale: CONFIG.locales.defaultLocale, - slug: ROUTES.BLOG, - }); - const schemaJsonLd = getSchemaJson([ - webpageSchema, - blogSchema, - breadcrumbSchema, + const jsonLd = getSchemaFrom([ + getWebPageGraph({ + breadcrumb: breadcrumbSchema, + description: messages.seo.metaDesc, + slug: ROUTES.BLOG, + title: messages.pageTitle, + }), + getBlogGraph({ + description: '', + posts: articles?.flatMap((page) => + page.edges.map(({ node }) => { + return { '@id': `${node.slug}#${ARTICLE_ID}` }; + }) + ), + slug: ROUTES.BLOG, + title: messages.pageTitle, + }), ]); const renderPaginationLabel: RenderPaginationItemAriaLabel = useCallback( @@ -266,14 +271,12 @@ const BlogPage: NextPageWithLayout = ({ +